/**
 * @author |\/| /\ |/ (
 */
 import com.marc.imageContent.*;
import com.marc.preloader.*;
import LuminicBox.Log.*;

class com.marc.imageContent.SlideShow {
	public var media:Array;
	public var slidesArray : Array;
	public var controlArray : Array;
	public var base:MovieClip;
	public var load_n:Number;
	public var baseholder:MovieClip;
	public var ctrlholder:MovieClip;
	public var currentNum : Number;
	public var delay:Number;
	
	private var preloader : Preloader; 
	private var preloader_mc : String;
	private var preloader_frame : String;
	private var gapX : Number;
	private var gapY : Number;
	private var btnName : String;
	private var btnDepth : Number;
	private var log:Logger;
	
	/**
	 * _base 
	 */
	public function SlideShow(_base:MovieClip,
							  _media:Array,
							  _preloader_mc:String,
							  _preloader_frame:String,
							  _controller:String,
							  _gapX,
							  _gapY,
							  _delay
							  ) {
		log=new Logger("slideshow");
        log.addPublisher( new ConsolePublisher() );
        preloader_mc=_preloader_mc;
        preloader_frame=_preloader_frame;
        slidesArray=new Array();
        load_n = 0;
        currentNum=0;
		base=_base;
		media=_media;
		delay=_delay;
		baseholder=base.createEmptyMovieClip("slideholder",1);
		ctrlholder=base.createEmptyMovieClip("ctrlholder",2);
		
		
		gapX=_gapX;
		gapY=_gapY;
		loadNextSlide();
				
		if(_controller != undefined || _controller != "none" || _controller != "" ){
			makeBtn(_gapX,_gapY,_controller);
		}
		
	}
	public function autoPlay(timeIntv):Void{
		log.warn("autoPlay");
		var intv:Number = setInterval(this,"showNext",timeIntv);
		if(timeIntv == 0){
			clearInterval(intv);
		}
	}
	public function showNext(){
		log.warn("next");
		if(load_n>1){
		if(currentNum<load_n){
			currentNum++;
		}else{
			currentNum=0;
		}
		showSlide(currentNum);
		}
	}
	public function loadAllSlides(base){
		var preloader:Preloader = new Preloader(slideholder, preloader_mc, 2, preloader_frame, 3);
		for (var i:Number = 0; i<media.length ; i++) {
				var slideholder:MovieClip=baseholder.createEmptyMovieClip("slideholder"+i,i);
				var pic:SlideImage = new SlideImage(slideholder, 1,String(media[i]), 0, 0, 450, 280);
				slidesArray.push(pic);			
				pic.loadImage();
				preloader.addToPreload(pic);
		}
	}
	public function makeBtn(_gapX,_gapY,_controller){
		for (var i:Number = 0; i<media.length ; i++) {
			//var btnobj:SlideBtn= new SlideBtn(ctrlholder,_controller,i);
			var btnobj=ctrlholder.attachMovie(_controller,"btn"+i,i);
			btnobj._x = i * _gapX;
			btnobj._y = i * _gapY;
			btnobj.sn = i;
			var o=this;  
			controlArray.push(btnobj);
		//	this.addListener(
			btnobj.onPress = function(){
				o.showSlide(this.sn);
			};
		}
	}
	public function loadNextSlide(){
		var o=this;
		if(load_n<media.length){
			var slideholder:MovieClip=baseholder.createEmptyMovieClip("slideholder"+load_n,load_n);
			var pic:SlideImage = new SlideImage(slideholder, 1,String(media[load_n]), 0, 0, 450, 280);
			var preloader:Preloader = new Preloader(slideholder, "preloader", 2, "preloader_frame", 3);
			slidesArray.push(pic);			
			pic.loadImage();
			
			preloader.addToPreload(pic);
			load_n++;
			//weird code here//
			//overwriting the dataReady function
			pic.dataReady=function(){
				log.info("pic.filename="+pic.filename==o.media[0]);
				if(pic.filename == o.media[0]){
					this.showUp();
					if(o.delay >0){
						o.autoPlay(o.delay);
						}
				}else{
					this.container_mc.mask_mc._width =0;
				}
			    o.loadNextSlide();
			};
			//weird code here//
		}
	}

	public function showSlide(_num){
		currentNum = _num;
		for(var i:Number = 0 ;i<slidesArray.length ; i++){
			if(i==_num){
				slidesArray[i].showUp();
			}else{
				slidesArray[i].disappear();
			}
			
		}
		
	}
}